package fr.upmc.i3s.upmcontact.tools;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.widget.NumberPicker;
import fr.upmc.i3s.upmcontact.R;
import fr.upmc.i3s.upmcontact.callbacks.IPickerCallback;

public class PickerDialogFactory {
	
	public static final int STAND_BY_LISTENER = 0;
	public static final int PICKED = 1;
	public static final int CANCELED = 2;

	public static AlertDialog createStringPickerForView(Context context, String title, int min, int max, int selected, final String[] displayedValues, final IPickerCallback callback) {

		callback.perform(STAND_BY_LISTENER, -1, null);

		// inflate number picker
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final NumberPicker npView = (NumberPicker) inflater.inflate(R.layout.number_picker, null);
		npView.setMinValue(min);
		npView.setMaxValue(max);
		npView.setValue(selected);
		npView.setDisplayedValues(displayedValues);

		// build alertdialog
		AlertDialog dialog = new AlertDialog.Builder(context)
		.setTitle(title)
		.setView(npView)
		
		.setPositiveButton(R.string.action_ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				callback.perform(PICKED, npView.getValue(), displayedValues[npView.getValue()]);
			}
		})
		
		.setNegativeButton(R.string.action_nok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				callback.perform(CANCELED, -1, null);
			}
		})
		
		.setOnCancelListener(new DialogInterface.OnCancelListener(){
			@Override
			public void onCancel(DialogInterface dialog) {
				callback.perform(CANCELED, -1, null);
			}
		})
		.create();
		return dialog;
	}

}
